package com.javaweb.blogcite.controller;

import com.javaweb.blogcite.aspect.JwtCheck;
import com.javaweb.blogcite.common.ResponseResult;
import com.javaweb.blogcite.controller.vo.ProjectView;
import com.javaweb.blogcite.service.ProjectService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class ProjectController {
    @Resource
    private ProjectService projectService;

    // 获取所有项目
    @GetMapping("/api/project")
    public ResponseResult<List<ProjectView>> getProject() {
        // 调用ProjectService的listAllProject方法获取所有项目
        List<ProjectView> project = projectService.listAllProject();
        // 创建ResponseResult对象
        ResponseResult<List<ProjectView>> responseResult = new ResponseResult<>();
        // 设置返回码为0
        responseResult.setCode(0);
        // 设置返回信息为success
        responseResult.setMsg("success");
        // 设置返回数据为project
        responseResult.setData(project);
        // 返回ResponseResult对象
        return responseResult;
    }

    @JwtCheck
    @DeleteMapping("/api/project/{id}")
    public ResponseResult deleteMessage(@PathVariable("id") Integer id) {
        boolean success = projectService.deleteProject(id);
        ResponseResult result = new ResponseResult();
        if (success) {
            result.setCode(0);
            result.setMsg("success");
        } else {
            result.setCode(500);
            result.setMsg("删除数据失败！");
        }
        return result;
    }

    @JwtCheck
    @PutMapping("/api/project/{id}")
    public ResponseResult<ProjectView> updateProject(@PathVariable("id") Integer id, @RequestBody ProjectView projectView) {
        boolean success = projectService.updateProject(id, projectView);
        ResponseResult result = new ResponseResult();
        if (success) {
            result.setCode(0);
            result.setMsg("success");
        } else {
            result.setCode(500);
            result.setMsg("失败！");
        }
        return result;
    }

    @JwtCheck
    @PostMapping("/api/project")
    public ResponseResult<ProjectView> addProject(@RequestBody ProjectView ProjectView) {
        boolean success = projectService.addProject(ProjectView);
        ResponseResult<ProjectView> result = new ResponseResult<>();

        if (success) {
            result.setCode(0);
            result.setMsg("success");
        } else {
            result.setCode(500);
            result.setMsg("失败！");
        }
        return result;
    }
}